﻿using JMcms.Common;
using JMcms.DBUtility;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace JMcms.Web.Plugin.Favorite.DAL
{
    /// <summary>
    /// 数据访问类:favorite
    /// </summary>
    public partial class favorite
    {
        private string databaseprefix; //数据库表名前缀
        public favorite(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "favorite");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int article_id, int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "favorite");
            strSql.Append(" where article_id=@article_id and user_id=@user_id");
            SqlParameter[] parameters = {
					new SqlParameter("@article_id", SqlDbType.Int,4),
					new SqlParameter("@user_id", SqlDbType.Int,4)};
            parameters[0].Value = article_id;
            parameters[1].Value = user_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.favorite model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + databaseprefix + "favorite(");
            strSql.Append("site_path,channel_name,article_id,article_title,user_id,user_name,add_time)");
            strSql.Append(" values (");
            strSql.Append("@site_path,@channel_name,@article_id,@article_title,@user_id,@user_name,@add_time)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@site_path", SqlDbType.NVarChar,100),
					new SqlParameter("@channel_name", SqlDbType.NVarChar,100),
					new SqlParameter("@article_id", SqlDbType.Int,4),
					new SqlParameter("@article_title", SqlDbType.NVarChar,100),
					new SqlParameter("@user_id", SqlDbType.Int,4),
                    new SqlParameter("@user_name", SqlDbType.NVarChar,100),
                    new SqlParameter("@add_time", SqlDbType.DateTime)};
            parameters[0].Value = model.site_path;
            parameters[1].Value = model.channel_name;
            parameters[2].Value = model.article_id;
            parameters[3].Value = model.article_title;
            parameters[4].Value = model.user_id;
            parameters[5].Value = model.user_name;
            parameters[6].Value = model.add_time;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.favorite model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + databaseprefix + "favorite set ");
            strSql.Append("site_path=@site_path,");
            strSql.Append("channel_name=@channel_name,");
            strSql.Append("article_id=@article_id,");
            strSql.Append("article_title=@article_title,");
            strSql.Append("user_id=@user_id,");
            strSql.Append("user_name=@user_name,");
            strSql.Append("add_time=@add_time");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@site_path", SqlDbType.NVarChar,100),
					new SqlParameter("@channel_name", SqlDbType.NVarChar,100),
					new SqlParameter("@article_id", SqlDbType.Int,4),
					new SqlParameter("@article_title", SqlDbType.NVarChar,100),
					new SqlParameter("@user_id", SqlDbType.Int,4),
                    new SqlParameter("@user_name", SqlDbType.NVarChar,100),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.site_path;
            parameters[1].Value = model.channel_name;
            parameters[2].Value = model.article_id;
            parameters[3].Value = model.article_title;
            parameters[4].Value = model.user_id;
            parameters[5].Value = model.user_name;
            parameters[6].Value = model.add_time;
            parameters[7].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + databaseprefix + "favorite set " + strValue);
            strSql.Append(" where id=" + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + databaseprefix + "favorite ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id, int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + databaseprefix + "favorite ");
            strSql.Append(" where id=@id and user_id=@user_id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@user_id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            parameters[1].Value = user_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.favorite GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,site_path,channel_name,article_id,article_title,user_id,user_name,add_time");
            strSql.Append(" from " + databaseprefix + "favorite");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.favorite model = new Model.favorite();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,site_path,channel_name,article_id,article_title,user_id,user_name,add_time ");
            strSql.Append(" FROM " + databaseprefix + "favorite ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM " + databaseprefix + "favorite");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        #region 私有方法================================
        private Model.favorite DataRowToModel(DataRow row)
        {
            Model.favorite model = new Model.favorite();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                    model.id = int.Parse(row["id"].ToString());
                if (row["site_path"] != null)
                    model.site_path = row["site_path"].ToString();
                if (row["channel_name"] != null)
                    model.channel_name = row["channel_name"].ToString();
                if (row["article_id"] != null && row["article_id"].ToString() != "")
                    model.article_id = int.Parse(row["article_id"].ToString());
                if (row["article_title"] != null)
                    model.article_title = row["article_title"].ToString();
                if (row["user_id"] != null && row["user_id"].ToString() != "")
                    model.user_id = int.Parse(row["user_id"].ToString());
                if (row["user_name"] != null)
                    model.user_name = row["user_name"].ToString();
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
            }
            return model;
        }
        #endregion
        #endregion
    }
}
